<?php

class Customers extends Controller {

	function customers()
	{
		parent::Controller();
		$this->load->library('table');
		$this->load->helper('array');
		$this->load->model('Customers_model');
		$this->load->model('Pawns_model');
		
	}

	function index()
	{
		// Displays the initial customers view
		
		redirect('customers/show');
	}
	
	function show()		
	{
		// Calls the Customers_model get() function, which returns an array of the 
		// complete customers table. It then sends this array to the show_view page.
		// If the URI contains a customer_id, it instead sets the $customer_id variable,
		// which tells the model's get() function to only return the record with the matching customer_id.
		// It then calls the update_view.
				
		$single_customer_view = FALSE;
		
		if ($this->uri->segment(3))		// check to see if there is a $customer_id field
		{
			$single_customer_view = TRUE; 
			$data=$this->Customers_model->general();
			$this->Customers_model->customer_id = $this->uri->segment(3);
			$this->Pawns_model->customer_id = $this->uri->segment(3);
			
			$data['pawn_result'] = $this->Pawns_model->get();
						
		}
		
		$data['result'] = $this->Customers_model->get();
		
		// Split up the date into the month, day, and year
		// and add these new values to the $data['result'] array
		if ($single_customer_view)
			list($data['result']->year, $data['result']->month, $data['result']->day) = split("-",$data['result']->dob);
			
		// Display the view
		$this->load->view('_global/header');  
		if ($single_customer_view)
			$this->load->view('customers/show_single_customer_view',$data);
		else
			$this->load->view('customers/show_all_customers_view',$data);
		$this->load->view('_global/footer');		
		
	}
	
	function add()		
	{
		// Adds or updates a new customer
		// query all the lookup tables and store their results in an array
		// send the results to the view, in order to populate the dropdown fields
		
		$this->load->helper('array');
		
		$data=$this->Customers_model->general();
	
		$data['action'] = 'Add';
		
		if ($this->uri->segment(3))		// if there is a $customer_id field, then it's an update
		{
			$this->Customers_model->customer_id = $this->uri->segment(3);
			$data['result'] = $this->Customers_model->get();
			
			$data['fgender']['select'] = $data['result']->customer_gender_id;
			$data['frace']['select'] = $data['result']->race_id;
			$data['fcity']['select'] = $data['result']->home_city_id;
			$data['fstate']['select'] = $data['result']->state_id;
			$data['feyecolor']['select'] = $data['result']->eye_color_id;
			$data['fhaircolor']['select'] = $data['result']->hair_color_id;
			$data['fphotoidtype']['select'] = $data['result']->photo_id_type_id;
			
			// Split up the date into the month, day, and year
			// and add these new values to the $data['result'] array
			list($data['result']->year, $data['result']->month, $data['result']->day) = split("-",$data['result']->dob);
			$data['action'] = 'Update';
		}
		
		$this->load->view('_global/header');
		$this->load->view('customers/update_customer_view',$data);
		$this->load->view('_global/footer');		
	}
	
	function save()
	{
		// Receives the add_customer_form result.
		// Takes the year,month,day fields from the form and concatenates them into the $dob field.
		// It then adds the $dob field to the _POST variable, and calls the model's save() function.
		// Finally, it displays all of the customers.
		
		$dob = $this->input->post('year') . $this->input->post('month') . $this->input->post('day');
		
		$_POST['dob'] = $dob;	
				
		$this->Customers_model->save($_POST);
		
		redirect('customers/show');
	}
	
	function delete()		// delete a customer
	{
		//call the Customers model delete_Customers function
		
		if ($this->uri->segment(3))
			$this->Customers_model->delete($this->uri->segment(3));
		
		redirect('customers/show');

	}
	
	function search()		// search for customers
	{
		if (isset($_POST['search']))
		{
			// query database with the $POSTED information
			// Call the customers/show method.
			$this->Customers_model->first_name = $this->input->post('first_name');
			$this->Customers_model->last_name = $this->input->post('last_name');
			$this->Customers_model->home_phone = $this->input->post('home_phone');
			
			$isSearch = TRUE;
			
			$data['result'] = $this->Customers_model->get();
			
			$this->load->view('_global/header');
			$this->load->view('customers/show_all_customers_view',$data);
			$this->load->view('_global/footer');
		}
		else
		{
			// display the search form
			$this->load->view('_global/header');
			$this->load->view('customers/search_view');
			$this->load->view('_global/footer');
		}
	}

}
?>
